package mhe.mgt.service;

import java.util.List;
import java.util.Map;

import mhe.mgt.daos.GenericDAO;
import mhe.mgt.daos.GenericDAOImpl;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

@SuppressWarnings("unchecked")
public abstract class GeneralServiceImpl<T> implements GeneralService<T> {

	private GenericDAO genericDAO = GenericDAOImpl.instance();
	
	protected final Log logger = LogFactory.getLog(getClass());
	
	public GenericDAO getGenericDAO() {
		return genericDAO;
	}

	public void setGenericDAO(GenericDAO genericDAO) {
		this.genericDAO = genericDAO;
	}

	public abstract Class<T> getEntityClass();

	@Override
	public List<T> list() {
		return (List<T>) genericDAO.list(getEntityClass());
	}

	@Override
	public T find(long id) {
		return (T) genericDAO.find(getEntityClass(), id);
	}

	@Override
	public List<T> list(String[] field, Object[] value, boolean andCompare,
			Map<String, Boolean> sortFields, int start, int length) {
		return (List<T>) genericDAO.list(getEntityClass(), field, value,
				andCompare, sortFields, start, length);
	}

	@Override
	public Object list(String[] fieldNames, Object[] fieldValues,
			boolean andCompare, Map<String, Boolean> sortFields, int start,
			int length, String loadField) {
		return genericDAO.list(getEntityClass(), fieldNames, fieldValues,
				andCompare, sortFields, start, length, loadField);
	}

	@Override
	public long count() {
		Long result = (Long) genericDAO.list(getEntityClass(), null, null,
				false, null, -1, -1, "count(*)").get(0);
		return result.longValue();
	}

	public long count(String[] field, Object[] value, boolean andCompare,
			Map<String, Boolean> sortFields) {
		Long result = (Long) genericDAO.list(getEntityClass(), field, value,
				andCompare, sortFields, -1, -1, "count(*)").get(0);
		return result.longValue();
	}

	protected void actionBeforeDelete(T entity) {
	}

	public void executeSQLQuery(String sql, Object[] values) {
		genericDAO.executeSQLQuery(sql, values);
	}

	@Override
	public T merge(T entity) {
		T result = (T) genericDAO.merge(entity);
		return result;
	}

	@Override
	public List<?> executeQuery(String sql, Object[] values, int start,
			int length) {
		return getGenericDAO().executeQuery(sql, values, start, length);
	}

	@Override
	public void add(T entity) {
		getGenericDAO().add(entity);
	}

	@Override
	public void update(T entity) {
		getGenericDAO().update(entity);
	}

	@Override
	public void delete(T entity) {
		actionBeforeDelete(entity);
		getGenericDAO().delete(entity);
	}

	protected boolean addWhereField(boolean addAnd, StringBuffer sql, String whereField) {
		if (addAnd) {
			sql.append("and ");
		} else {
			sql.append("where ");
			addAnd = true;
		}
		sql.append(whereField);
		return addAnd;
	}

	protected boolean isValid(Integer num) {
		return num != null && num != 0;
	}
	
	protected boolean isValid(Long num) {
		return num != null && num != 0;
	}

	protected boolean isValid(String s) {
		return s != null && !s.isEmpty();
	}

}
